package com.situation.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.github.pagehelper.PageInfo;
import com.situation.annotation.ControllerEndpoint;
import com.situation.mapper.TbImageMapper;
import com.situation.pojo.DO.TbImage;
import com.situation.service.TbImageService;
import com.situation.util.OSSUtil;
import com.situation.util.OSSUtilTemp;
import com.situation.util.RespUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * @author TaoMinghui
 * @create 2021-12-08 11:39
 */
@RestController
@RequestMapping("/api/upload")
public class TbImageController {

    @Autowired
    private TbImageService tbImageService;
    @Autowired
    private TbImageMapper tbImageMapper;

    /**
     * 附件上传
     *
     * @param fjs
     * @return
     * @throws IOException
     */
    @ControllerEndpoint(exceptionMessage = "添加图片失败", operation = "添加图片")
    @RequiresPermissions({"upload:image"})
    @PostMapping("/image")
    public RespUtil findImageList(MultipartFile[] fjs) throws IOException {

        for (MultipartFile fj : fjs) {

            //获取原文件名
            String originalFilename = fj.getOriginalFilename();
            //获取原文件类型
            String contentType = fj.getContentType();
            //获取文件大小
            long size = fj.getSize() / 10000;
            //获取文件后缀名
            String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);

            //图片宽高

            BufferedImage bufferedImage = ImageIO.read(fj.getInputStream());
            int width=0;
            int height=0;
            if (bufferedImage != null) {
                width = bufferedImage.getWidth();
                height = bufferedImage.getHeight();

            }
            //写入文件
            //重命名：防止覆盖
            //将fj上传到OSS的image目录上
            String newFjName = OSSUtilTemp.putObjectToOSS(fj, "image");
            //将图片相关信息传入tb_image
            TbImage tbImage = new TbImage((long) 0, newFjName, size, contentType, suffix, height, width, LocalDateTime.now());
            System.out.println(tbImage.toString());
            tbImageService.addTbImage(tbImage);

        }


        return RespUtil.ok();
    }

    /**
     * 多条件分页模糊查询附件列表
     *
     * @param pageNum
     * @param pageSize
     * @param tbImage
     * @return
     */
    @GetMapping("/findImageList")
    public RespUtil findImageList(int pageNum, int pageSize, TbImage tbImage) {
        PageInfo<TbImage> imageListFenYeMoHu = tbImageService.findImageListFenYeMoHu(pageNum, pageSize, tbImage);
        return RespUtil.ok().setData(imageListFenYeMoHu);
    }

    /**
     * 通过id删除一个附件图片
     *
     * @param id
     * @return
     * @throws FileNotFoundException
     */
    @ControllerEndpoint(exceptionMessage = "删除图片失败", operation = "删除图片")
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("attachment:delete")
    public RespUtil delete(@PathVariable int id) throws FileNotFoundException {
        //获取需要删除的图片的path
        String path = tbImageMapper.findPathById(id);
        System.out.println(path);
        String folder = "image/" + path;
        System.out.println(folder);
        OSS ossClient = OSSUtilTemp.getOssClient();
        //删除OSS服务器上的图片
        ossClient.deleteObject("eda02", folder);
        //删除图片在数据库中的记录
        tbImageService.deleteImageById(id);
        return RespUtil.ok();
    }
}
